package com.demo.modules.pay.controller;

import com.demo.common.core.exception.ServiceException;
import com.demo.common.core.result.R;
import com.demo.modules.pay.config.AliPayProperties;
import com.demo.modules.pay.config.WxPayProperties;
import com.demo.modules.pay.domain.pay.PayParam;
import com.lly835.bestpay.config.AliPayConfig;
import com.lly835.bestpay.config.WxPayConfig;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @author MyPC
 */
@Api(tags = "支付模块")
@RequestMapping("/pay")
@RestController
public class PayController {

    @Autowired
    private WxPayProperties wxPayProperties;

    @Autowired
    private AliPayProperties aliPayProperties;

    @ApiOperation("开始支付")
    @PostMapping("/startPay")
    public R startPay(@RequestBody @Valid PayParam payParam) {
        PayRequest prq = new PayRequest();
        //支付类型
        prq.setPayTypeEnum(payParam.getPayType());
        //金额
        prq.setOrderAmount(payParam.getAmount().doubleValue());
        prq.setOrderName("订单名称");
        prq.setAttach("备注");
        prq.setOrderId(payParam.getOrderId());
        //申请支付订单
        BestPayServiceImpl bestPayService = payService(payParam);
        PayResponse prs = bestPayService.pay(prq);
        return R.ok(prs);
    }

    private BestPayServiceImpl payService(PayParam payParam) {
        BestPayServiceImpl bestPayService = new BestPayServiceImpl();
        switch (payParam.getPayType()) {
            case WXPAY_NATIVE:
                WxPayConfig wxPayConfig = new WxPayConfig();
                wxPayConfig.setAppId(wxPayProperties.getMpAppId());
                wxPayConfig.setAppSecret(wxPayProperties.getAppSecret());
                wxPayConfig.setMchId(wxPayProperties.getMchId());
                wxPayConfig.setMchKey(wxPayProperties.getMchKey());
                wxPayConfig.setKeyPath(wxPayProperties.getKeyPath());
                wxPayConfig.setNotifyUrl(wxPayProperties.getNotifyUrl());
                bestPayService.setWxPayConfig(wxPayConfig);
                break;
            case ALIPAY_PC:
                AliPayConfig aliPayConfig = new AliPayConfig();
                aliPayConfig.setAppId(aliPayProperties.getAppId());
                aliPayConfig.setPrivateKey(aliPayProperties.getPrivateKey());
                aliPayConfig.setAliPayPublicKey(aliPayProperties.getAlipayPublicKey());
                aliPayConfig.setNotifyUrl(aliPayProperties.getNotifyUrl());
                aliPayConfig.setReturnUrl(aliPayProperties.getReturnUrl());
                aliPayConfig.setSandbox(aliPayProperties.getSandbox());
                bestPayService.setAliPayConfig(aliPayConfig);
                break;
            default:
                throw new ServiceException("暂不支持的支付类型");
        }
        return bestPayService;
    }
}
